package com.LeetCode.StackAndHeap;

import javafx.util.Pair;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.PriorityQueue;

/**
 * 前 k 高频率的词
 */
public class TopKFOfWords {



    public List<String> topKFrequent(String[] words, int k) {
        HashMap<String , Integer> map = new HashMap<>();
        PriorityQueue<String> workCountHeap
                = new PriorityQueue<>(
                        (a, b) -> map.get(a).equals(map.get(b)) ?
                        a.compareTo(b) : map.get(b) - map.get(a));
        for(String word : words){
            map.put(word,map.getOrDefault(word,0)+1);
        }
        for(String word : map.keySet()){
            workCountHeap.offer(word);
        }
        ArrayList<String> ans = new ArrayList<>();
        for(int i = 0; i < k; i++){
            ans.add(workCountHeap.poll());
        }
        return ans;
    }

    @Test
    public void test(){
        String[] strs = {"i", "love", "leetcode", "i", "love", "coding"};
        int k = 2;
        topKFrequent(strs,k).forEach(e -> System.out.print(e+" "));
    }
}
